<template>
	<div class="front">
		<el-input v-model="searchText" placeholder="输入关键字" style="margin: 10px 0!important"></el-input>
		<div class="tree bj bg-white">
            <el-scrollbar style="height: 100%;">
			     <el-tree :data="treeData" accordion node-key="id" :default-expanded-keys="dfExpKeys" :props="defaultProps" :filter-node-method="filterNode" ref="tree" @node-click="nodeClick" :default-expand-all="true" highlight-current :expand-on-click-node="false"></el-tree>
            </el-scrollbar>
		</div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			searchText: '',
			treeData: [],
			dfExpKeys: [],
			defaultProps: {
				label: 'name',
                children: 'subDepartment'
			}
		}
	},
	watch: {
        searchText(val){
            this.$refs.tree.filter(val);
        }
    },
    computed: {
    	company() {
    		return this.$store.state.companyName;
    	},
    	systemId() {
    		return this.$store.state.systemId;
    	}
    },
	methods: {
		getTreeData() {     //获取树形结构数据
            let data = {
                name: this.company,
                id: this.systemId,
                level: 0,
                subDepartment: []
            }
            this.$http.get('department/selectObject?systemId=' + this.systemId).then(res => {
                if(res.data.code == 20001) {
                    if(res.data.data) {
                        data.subDepartment = res.data.data;
                        this.dfExpKeys.push(data['id']);
                    }
                }
            });
            this.treeData = [data];
            // //设置高亮
            setTimeout(() => {
                this.$refs.tree.setCurrentKey(this.dfExpKeys.join(''));
            }, 1000);
        },
		nodeClick(row) {
			this.$store.commit('customerStore/updateParentId', row['id']);
			this.$store.commit('customerStore/updateParentName', row['name']);
			this.close();
		},
		filterNode(value, data) {               //部门选择里面的搜索框
            if (!value) return true;
            return data.name.indexOf(value) !== -1;
        },
        close() {           //关闭新增弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        }
	},
	created() {
		this.getTreeData();
	}
}
</script>
<style scoped>
	.tree {height: calc(100% - 60px);box-sizing: border-box;padding: 5px 0;}
	.tree .el-tree {height: 100% !important;overflow-y: auto !important;}
</style>